﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Users;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class WishList
    {
        public static void AddProduct(int PortalID, string ProductID, UserInfo UserInfo)
        {
            Hashtable hashtable = new Hashtable();
            string str = "";
            string[] strArray = GetIDList(PortalID).Split(new char[] { ',' });
            int upperBound = strArray.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                hashtable.Add(strArray[i], strArray[i]);
            }
            if (!hashtable.ContainsValue(ProductID))
            {
                hashtable.Add(ProductID, ProductID);
            }
            IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
            while (enumerator.MoveNext())
            {
                DictionaryEntry current = (DictionaryEntry) enumerator.Current;
                str = str + "," + current.Value.ToString();
            }
            str = str.Trim(new char[] { ',' });
            SharedFunctions.setStoreCookieValue(PortalID, "WishList", "ProductList", str, 30.0);
            if ((UserInfo != null) && (EventInterface.Instance() != null))
            {
                EventInterface.Instance().AddToWishList(PortalID, Conversions.ToInteger(ProductID), UserInfo);
            }
        }

        public static void ClearList(int PortalID)
        {
            SharedFunctions.removeStoreCookie(PortalID, "WishList");
        }

        public static string GetIDList(int PortalID)
        {
            return SharedFunctions.getStoreCookieValue(PortalID, "WishList", "ProductList");
        }

        public static string GetItemCount(int PortalID)
        {
            return GetItemCountInt(PortalID).ToString();
        }

        public static int GetItemCountInt(int PortalID)
        {
            if (GetIDList(PortalID) == "")
            {
                return 0;
            }
            return GetIDList(PortalID).Split(new char[] { ',' }).GetLength(0);
        }

        public static ArrayList GetList(int PortalID)
        {
            ArrayList list = new ArrayList();
            ProductController controller = new ProductController();
            string[] strArray = GetIDList(PortalID).Split(new char[] { ',' });
            int upperBound = strArray.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                if (Versioned.IsNumeric(strArray[i]))
                {
                    ProductListInfo productListInfo = controller.GetProductListInfo(Conversions.ToInteger(strArray[i]), SharedFunctions.GetCurrentCulture());
                    if (productListInfo != null)
                    {
                        list.Add(productListInfo);
                    }
                }
            }
            return list;
        }

        public static bool IsInWishlist(int PortalID, string ProductID)
        {
            Hashtable hashtable = new Hashtable();
            string[] strArray = GetIDList(PortalID).Split(new char[] { ',' });
            int upperBound = strArray.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                hashtable.Add(strArray[i], strArray[i]);
            }
            return hashtable.ContainsValue(ProductID);
        }

        public static void RemoveProduct(int PortalID, string ProductID)
        {
            Hashtable hashtable = new Hashtable();
            string str = "";
            string[] strArray = GetIDList(PortalID).Split(new char[] { ',' });
            int upperBound = strArray.GetUpperBound(0);
            for (int i = 0; i <= upperBound; i++)
            {
                hashtable.Add(strArray[i], strArray[i]);
            }
            if (hashtable.ContainsValue(ProductID))
            {
                hashtable.Remove(ProductID);
            }
            IDictionaryEnumerator enumerator = hashtable.GetEnumerator();
            while (enumerator.MoveNext())
            {
                DictionaryEntry current = (DictionaryEntry) enumerator.Current;
                str = str + "," + current.Value.ToString();
            }
            str = str.TrimStart(new char[] { ',' });
            SharedFunctions.setStoreCookieValue(PortalID, "WishList", "ProductList", str, 30.0);
        }
    }
}

